package pracise2020615注解;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;

 class Student {

    @StudentName("张三")
    private String name;

    @Gender(gender = Gender.GenderType.Male)
    private String gender;

    @Homeaddress(id = 442000, city = "十堰市", address = "红十字新村")
    private String homeAddress;

    //...get/set 略
}


@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface StudentName{
    String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Gender{
    enum GenderType{
        Male("男"),Faemale("女");
        private String genderStr;
        private GenderType(String genderStr) {
            this.genderStr = genderStr;
        }

        @Override
        public String toString() {
            return  genderStr ;
        }
    }

    public GenderType gender();

}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Homeaddress{
    int id();
    String city();
    String address();
}

class StudentUitl{
    static void getStudent(Class classa){
        String name="";
        String gender="";
        String homeAddress="";

        Field fields[] = classa.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(StudentName.class)) {
                StudentName arg0 = field.getAnnotation(StudentName.class);
                name = name + arg0.value();
                System.out.print( "姓名:" + name+"\t");
            }
            if (field.isAnnotationPresent(Gender.class)) {
                Gender arg0 = field.getAnnotation(Gender.class);
                gender = gender + arg0.gender().toString();
                System.out.print("性别:" + gender+"\t");
            }
            if (field.isAnnotationPresent(Homeaddress.class)) {
                Homeaddress arg0 = field.getAnnotation(Homeaddress.class);
                homeAddress =  arg0.city() +  arg0.address()+"(邮编"+arg0.id()+")";
                System.out.print("家庭住址:" + homeAddress+"\t");
            }
        }

    }
}
    public class TestStudent{
        public static void main(String[] args){
            StudentUitl.getStudent(Student.class);
        }
    }

